
import { requestApi } from '../../../api';
const app = getApp();
Page({
    data: {
        brands: [],
        config: {},
        bids: ''  // 当前已选中的品牌ID
    },
    onLoad: function (options) {
        const multiple = Boolean(options && options.multiple) || false;
        const checked = options && options.bids || '';

        if ( !multiple ) {
            // 默认是请求所有的品牌列表
            this.getAllBrandsList();
        } else {
            // 若是多选，则是从筛选的列表跳转过来，不需要请求所有的品牌列表
            this.setData({
                brands: app.data.searchBrands,
                bids: checked,
                config: {
                    multiple
                }
            });
        }
    },
    getAllBrandsList() {
        const { brands } = this.data;
        wx.showLoading({
            title: '加载中...',
        });

        app.request({
            url: requestApi.allBrandsList,
            data: {
                is_new_ver: true
            }
        }, (d) => {
            if (d.original.ret_code == 200) {
                d.original.data.item_list.forEach((item, index) => {
                    if (item.type == 331) {
                        brands.push(item.data);
                    }
                })

                this.setData({
                    brands
                });
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading();
        })
    },
    chooseBrand(e) {
        const { name } = e.detail;

        wx.navigateTo({
            url: '/pages/categoryHall/categoryHall?keywords=' + name
        })
    },
    getBrandsIds(e) {
        wx.navigateBack({delta: 1});
    }
})